<!--
 * @Description：授权API功能--授权列表
 * @author： wlj
 * @date： 2025/10/14 14:28:08
!-->
<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item label="授权ID">
        <el-input v-model="listQuery.clientId" placeholder="请输入授权ID,回车搜索" @keyup.enter.native="handleResetPage">
          <el-button slot="append" icon="el-icon-search" @click="handleResetPage" />
        </el-input>
      </el-form-item>
      <el-form-item label="授权密钥">
        <el-input v-model="listQuery.clientSecret" placeholder="请输入授权密钥,回车搜索" @keyup.enter.native="handleResetPage">
          <el-button slot="append" icon="el-icon-search" @click="handleResetPage" />
        </el-input>
      </el-form-item>
      <el-form-item label="授权系统名称">
        <el-input v-model="listQuery.clientName" placeholder="请输入授权系统名称,回车搜索" @keyup.enter.native="handleResetPage">
          <el-button slot="append" icon="el-icon-search" @click="handleResetPage" />
        </el-input>
      </el-form-item>
      <el-button type="primary" @click="handleAddRole()">新增</el-button>
    </el-form>
    <el-table v-loading="listLoading" :data="tableList" border fitedit stripe>
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="授权ID" prop="clientId" show-overflow-tooltip />
      <el-table-column label="授权密钥" prop="clientSecret" show-overflow-tooltip />
      <el-table-column label="授权系统名称" prop="clientName" show-overflow-tooltip />
      <el-table-column label="授权系统logo" prop="previewFileClientLogo" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-image class="img-size" :src="getImgUrl(scope.row.previewFileClientLogo)" fit="cover" :preview-src-list="[getImgUrl(scope.row.previewFileClientLogo)]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="文件API接口" prop="fileApi" show-overflow-tooltip />
      <el-table-column label="文件API请求方法" prop="fileApiMethod" show-overflow-tooltip />
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip />
      <el-table-column width="100" label="操作" align="center">
        <template slot-scope="scope">
          <el-dropdown class="operation-style">
            <span class="el-dropdown-link">  操作<i class="el-icon-arrow-down el-icon--right"></i>  </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div v-if="count > 0">
      <pagination
        :total="count"
        layout="total,sizes, prev, pager, next, jumper"
        :page.sync="listQuery.page.pageIndex"
        :limit.sync="listQuery.page.pageSize"
        @pagination="search"
      />
    </div>
    <FormDialog v-if="addShow" :dis-type="type" :dialog-title="dialogTitle" :edit-param="editParam" @event="parentEvent" />
  </div>
</template>

<script>
import store from '@/store'
import FormDialog from './com/FormDialog'
import { iamAuthPage, iamAuthDelete } from '@/api/authAPIFun/index'
export default {
  components: { FormDialog },
  data() {
    return {
      fullscreenLoading: false,
      listLoading: false,
      listQuery: {
        clientId: '',
        clientSecret: '',
        clientName: '',
        page: {
          pageIndex: 1,
          pageSize: 20
        }
      },
      count: 0,
      tableList: [],
      type: '',
      addShow: false,
      dialogTitle: '',
      editParam: {}
    }
  },
  created() {
    this.search()
  },
  methods: {
    // 获取图片路径
    getImgUrl(url) {
      return this.global.baseApi + url + `&token=${store.getters.token}`
    },
    search() {
      this.listLoading = true
      iamAuthPage(this.listQuery).then(res => {
        if (res.success) {
          this.tableList = res.data.rows
          this.count = res.data.total
          this.listLoading = false
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 点击新增
    handleAddRole() {
      this.type = 'add'
      this.dialogTitle = '新增'
      this.addShow = true
    },
    // 点击编辑
    handleEdit(row) {
      this.editParam = row
      this.type = 'edit'
      this.dialogTitle = '编辑'
      this.addShow = true
    },
    // 关闭--弹窗
    parentEvent(flag) {
      this.addShow = false
      this.search()
    },
    // 删除角色 （over）
    handleDelete(row) { // 单个删除
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDelete([row.clientId])
      }).catch(() => {})
    },
    // 执行删除操作
    doDelete(ids) {
      iamAuthDelete(ids).then(res => {
        if (res.success) {
          this.$message({ message: '删除成功', type: 'info' })
          this.search()
        } else this.$message.error(res.message)
      })
    },
    // 切换搜索条件时，页码重置
    handleResetPage() {
      this.listQuery.page.pageIndex = 1
      this.search()
    }
  }
}
</script>
<style lang="scss" scoped>
.img-size{
  width: 40px;
  height: 40px;
}
</style>

